package com.dmooo.yxmmerchant.view.model;

import android.content.Context;

import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.mywidget.LoadingDialog;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.dmooo.yxmmerchant.view.contract.MerchantAlbContract;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

public class MerchantAlbModel implements MerchantAlbContract.MerchantAlbMdl {
    private Context context;
    private LoadingDialog loadingDialog;
    public MerchantAlbModel(Context context) {
        this.context=context;
        loadingDialog=new LoadingDialog(context);
    }

    @Override
    public void getImgList(String token, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"登录中"),map);
        HttpManager.getInstance().getImgList(post);
    }

    @Override
    public void addImg(String token, List<File> img,final HttpOnNextListener listener) {
        try {
            RequestParams requestParams = new RequestParams();
            AsyncHttpClient client1 = new AsyncHttpClient();
            client1.addHeader("token", token);
            requestParams.put("token", token);
            for (int i=0;i<img.size();i++) {
                requestParams.put("img["+i+"]",img.get(i));
            }
            client1.post(Config.APP_INTERFACE_BASE_URL + "/merchant.php?c=MerchantImg&a=add", requestParams, new TextHttpResponseHandler() {

                @Override
                public void onStart() {
                    super.onStart();
                    loadingDialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    listener.onFail(responseString);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    LogUtil.e("backdata",responseString);
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("code")==0){
                            listener.onNext("");
                        }else{
                            listener.onFail(object1.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }catch (FileNotFoundException e){
            LogUtil.e("backdata",e.toString());
        }
    }

    @Override
    public void delImg(String token, String id, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("id",id);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"登录中"),map);
        HttpManager.getInstance().delAlbImg(post);
    }

    @Override
    public void getNotice(String token, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"登录中"),map);
        HttpManager.getInstance().getNotice(post);
    }

    @Override
    public void editNotice(String token, String content, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("content",content);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"登录中"),map);
        HttpManager.getInstance().edtiNotice(post);
    }
}
